$(function(){
	var cookieName = $.cookie('username')
	if(cookieName){
		var header = '<img src="uploads/' + $.cookie('header') + '" onerror="this.src=\'images/user.png\'">'
		$('#user').empty().html(header+cookieName)
	}
	 $('.dropdown-menu li').last().click(function(){
    	$.removeCookie('username')
    	location.reload()
    })
	 $('#user').click(function(){
	 	if(!cookieName){
	 		$(this).removeAttr('data-toggle')
	 		location.href = '/signin'
	 	}
	 })
	 
	 //提问跳转部分
	 $('#ask').click(function(){
	 	if(cookieName){
	 		location.href = '/ask'
	 	}
	 	else{
	 		$('.modal-body p ').text('请先登录')
	 		$('.modal').modal('show')
	 	}
	 })
	 
	 //回答跳转部分
	 $('.question').delegate('.answer','click',function(){
    	if(cookieName){
        	var $id = $(this).data('id')
        	$.cookie('questionId',$id)
            location.href = '/answer'
        }else{
            $('.modal-body p').text('请先登录...')
            $('.modal').modal('show')
        }
    })
 
     //删除功能
     $('.modal-close').click(function(){
        $('.modal').modal('hide')
        .on('hidden.bs.modal',function(){
			$.get('/remove',function(data){
				if(cookieName){
					if(data.code=='success'){
					location.href = '/'
					}else{
						alert(data.message)
					}	
				}else{
					location.href = '/signin'
				}
			})
        })
    })

	 $('.question').delegate('.remove','click',function(){
		 var $id = $(this).data('id')
		$.cookie('questionId',$id)
		if(cookieName){
			$.get('/same',function(data){
				console.log(data)
				if(data.code=='success'){
					$('.modal-body p').text('确认删除?')
					$('.modal').modal('show')
				}else{
					alert(data.message)
				}
			})
          
		}else{
			  $('.modal-body p').text('请先登录...')
              $('.modal').modal('show')
		}
	})

    
    
    //分页功能
    var page = $('.pagination .active').index()
	$('.pagination li').first().click(function(){
		if(page > 1){
			page--
			location.href = '/?page=' + page
		}
	})
	$('.pagination li').last().click(function(){
		if(page < $(this).index() -1) {
			page++
			location.href = '/?page=' + page
		}
	})
	
	$('.pagination .active').click(function(ev){
		ev.preventDefault()
	})
    
   
})